package com.flute.framework.autoprompt;

import com.flute.framework.configuration.MonitorXMLConverter;
import com.flute.framework.exception.ConfigurationParseException;
import com.flute.framework.monitor.EventLogger;
import com.flute.framework.monitor.FileHierarchy;
import com.flute.framework.monitor.MonitorService;
import com.flute.tools.util.ExceptionUtil;

public class AutoPromptMonitor {
	private static MonitorService service;
	private static final String DATA_UPDATE = "prompt-data-update";
	private static final String DATA_INITIAL = "prompt-data-initial";

	static {
		try {
			service = new MonitorService(MonitorXMLConverter.class
					.getClassLoader().getResourceAsStream("config/monitor.xml"));
		} catch (ConfigurationParseException e) {
			EventLogger.error("加载monitor配置文件失败."
					+ ExceptionUtil.getExceptionContent(e),
					EventLogger.MONITOR_KEY);
		}

		try {
			service.start();
		} catch (Exception e) {
			EventLogger.error("注册事件到服务器失败."
					+ ExceptionUtil.getExceptionContent(e),
					EventLogger.MONITOR_KEY);
		}
		FileHierarchy.setPath(service.getDefinition().getLocation());
	}

	public static String getAutoPromptKey(String key) {
		if (!service.getDefinition().containsEvent(key)) {
			EventLogger.error("monitor not defined, '" + key + "'",
					EventLogger.MONITOR_KEY);
		}
		return key;
	}

	public static String getUpdateKey() {
		if (!service.getDefinition().containsEvent(DATA_UPDATE)) {
			EventLogger.error("monitor not defined, '" + DATA_UPDATE + "'",
					EventLogger.MONITOR_KEY);
		}
		return DATA_UPDATE;
	}

	public static String getInitialKey() {
		if (!service.getDefinition().containsEvent(DATA_INITIAL)) {
			EventLogger.error("monitor not defined, '" + DATA_INITIAL + "'",
					EventLogger.MONITOR_KEY);
		}
		return DATA_INITIAL;
	}
}
